package com.cdut.logistics.action.datacentre;

import java.util.ArrayList;
import java.util.List;

import com.cdut.logistics.intercepter.system.AuthorityIntercepter;
import com.cdut.logistics.model.dao.datacentre.Academic;
import com.cdut.logistics.service.impl.datacentre.AcademicImpl;
import com.cdut.logistics.service.interf.datacentre.AcademicService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

/**  
 * @ClassName: UserController  
 * @Description: 学院管理控制类
 * @author wxt.xia
 * @date 2018年1月26日 下午3:28:25    
 */
//@Before(AuthorityIntercepter.class)
public class AcademicController extends Controller {
	
	private AcademicService academicService = new AcademicImpl();
	
	
	/**
	 * 
	 * @Title: queryById  
	 * @Description: 根据学院id查询学院
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void queryById(){
		/*
		 * 解析json
		 */
		int id = Integer.valueOf((String) getPara("academy_id"));
		System.out.println("学院id  :"+id);
		Record academic = new Record();
		try {
		    academic = academicService.queryById(id);
			System.out.println(academic.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(academic);
	}
	
	/**
	 * 
	 * @Title: query  
	 * @Description: 根据学院名称查询学院   
	 * @author wxt.xia
	 * @param json
	 * @throws   
	 */
	public void query(){
		/*
		 * 解析json
		 */
		String name = getPara("academy_name")+"%";
		List<Record> academicList = new ArrayList<Record>();
		try {
			academicList = academicService.query(name);
			System.out.println(academicList.get(0).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(academicList);
	}
	
	/**
	 * 
	 * @Title: queryBySchool  
	 * @Description: 根据学校id查询学院
	 * @author wxt.xia
	 * @throws
	 */
	public void queryBySchool(){
		/*
		 * 解析json
		 */
		int id = Integer.valueOf((String) getPara("school_id"));
		List<Academic> academicList = new ArrayList<Academic>();
		try {
			academicList = academicService.queryBySchool(id);
			System.out.println(academicList.get(0).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(academicList);
	}
	
	/**
	 * 
	 * @Title: add  
	 * @Description: 新增学院
	 * @author wxt.xia
	 * @throws
	 */
	public void add(){
		/*
		 * 解析json
		 */
		
		//下面是测试数据，id需设置为自增长
		Academic academic = new Academic();
		academic.set("name", "信息科学与技术学院");
		academic.set("school_id", "1");
		
		try {
			System.out.println("控制类*************");
			academicService.add(academic);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: form  
	 * @Description: 修改学院
	 * @author wxt.xia
	 * @throws
	 */
	public void modify(){
		/*
		 * 解析json
		 */
		Academic academic = new Academic();
		academic.set("name", "信息科学与技术学院");
		academic.set("id", 2);
		Academic dataAcademic = academicService.get((Integer)academic.get("id"));
		dataAcademic.set("name",academic.get("name"));
		try {
			academicService.modify(dataAcademic);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: remove  
	 * @Description: 删除学院
	 * @author wxt.xia
	 * @throws
	 */
	public void remove(){
		int id = 3;
		try {
			academicService.remove(id);
		} catch (Exception e) {
			
		}
	}
}
